package com.useeinfo.testgit;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();

    private Button btn;
    OkHttpClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        Log.e(TAG, "onCreate: =" + System.currentTimeMillis());
        // TODO: 2017/4/12 14:42 Remember here are some things you need to do  测试AS提交
//        Log.e(TAG, "onCreate: ===========");

        btn = (Button) findViewById(R.id.button);


        test1();
//
//        test2();

    }



    private void test1() {
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Request request = new Request.Builder()
                        .url("http://118.178.87.236:8080/lcdata//api/check/getSecondQuarantineApplicationList.action")
                        .get()
                        .addHeader("userId", "13")
                        .header("User-Agent", "OkHttp Example")
                        .header("userId", "13")
                        .build();

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Response response = null;
                        try {
                            response = client.newCall(request).execute();
                            if (response.isSuccessful()) {
                                Log.e(TAG, "打印POST响应的数据：" + response.body().string());
                            } else {
                                throw new IOException("Unexpected code " + response);
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

            }
        });

//        CookieJarImpl cookieJar = new CookieJarImpl(new PersistentCookieStore(getApplicationContext()));
        client = new OkHttpClient.Builder()
                .addInterceptor(new LoggingInterceptor())
//                .cookieJar(cookieJar)
                .build();

        RequestBody formBody = new FormBody.Builder()
                .add("username", "bxd")
                .add("password", "1234")
                .build();

        final Request request = new Request.Builder()
                .url("http://118.178.87.236:8080/lcdata/api/check/user/login.action")
                .post(formBody)
                .header("User-Agent", "OkHttp Example")
                .build();

        new Thread(new Runnable() {
            @Override
            public void run() {
                Response response = null;
                try {
                    response = client.newCall(request).execute();
                    if (response.isSuccessful()) {
                        Log.e(TAG, "打印POST响应的数据：" + response.body().string());
                    } else {
                        throw new IOException("Unexpected code " + response);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    private void test2() {
        OkHttpUtils.post().url("http://118.178.87.236:8080/lcdata/api/check/user/login.action")
                .addParams("username", "bxd")
                .addParams("password", "1234")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e(TAG, "onError: e.toString()=" + e.toString());
                        Log.e(TAG, "onError: e.getMessage()=" + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        //{"status":1,"msg":"账号密码有误","contents":""}
                        //{"status":0,"msg":"成功","contents":"","role":"leader"}
                        Log.e(TAG, "onSuccess: response==" + response);
                    }
                });


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                OkHttpUtils.get().url("http://118.178.87.236:8080/lcdata//api/check/getSecondQuarantineApplicationList.action")
                        .build().execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e(TAG, "setOnClickListener onError: e.toString()=" + e.toString());

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e(TAG, "setOnClickListener onSuccess: response==" + response);

                    }
                });
            }
        });



    }


    class LoggingInterceptor implements Interceptor {
        @Override
        public Response intercept(Interceptor.Chain chain) throws IOException {
            Request request = chain.request();

            long t1 = System.nanoTime();
            Log.e(TAG, "intercept: 1111=" + String.format("Sending request %s on %s%n%s",
                    request.url(), chain.connection(), request.headers()));


            Response response = chain.proceed(request);

            long t2 = System.nanoTime();
            Log.e(TAG, "intercept: 1111=" + String.format("Received response for %s in %.1fms%n%s",
                    response.request().url(), (t2 - t1) / 1e6d, response.headers()));

            return response;
        }
    }


}
